package cn.wjzhang.middle.modules.geoLite.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.wjzhang.core.exception.BootException;
import cn.wjzhang.core.utils.IPUtils;
import cn.wjzhang.middle.modules.bank.beans.entity.MidBankLhh;
import cn.wjzhang.middle.modules.bank.beans.vo.MidBankLhhVo;
import cn.wjzhang.middle.modules.geoLite.beans.vo.GeoLiteVo;
import cn.wjzhang.middle.modules.geoLite.mapper.GeoLiteMapper;
import cn.wjzhang.middle.modules.geoLite.service.GeoLiteService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * ========================
 * GeoLite2
 *
 * @author wjzhang
 * @version v1.0
 * ========================
 * @date 2023/7/14 10:45
 */
@Service
public class GeoLiteServiceImpl implements GeoLiteService {
    @Resource
    private GeoLiteMapper geoLiteMapper;

    @Override
    public GeoLiteVo getGeoLiteByIp(HttpServletRequest request, String ip) {
        GeoLiteVo geoLiteByIp = null;
        if (StrUtil.isNotBlank(ip)) {
            geoLiteByIp = geoLiteMapper.getGeoLiteByIp(ip);
        }else {
            String clientIp = IPUtils.getClientIp(request);
            geoLiteByIp = geoLiteMapper.getGeoLiteByIp(clientIp);
        }
        if (ObjectUtil.isEmpty(geoLiteByIp)) {
            throw new BootException("未查询到ip详情");
        }
        return geoLiteByIp;
    }
}
